import { ref, type Ref } from 'vue';
import { defineStore } from 'pinia';
import { parse, stringify } from 'zipson'

type State = {
    token: Ref<string>
    userInfo: any
    permissions: Ref<string[]>
    currentRouter: Ref<string>
    setToken: (value: string) => void
    removeToken: () => void
    setUserInfo: (value: any) => void
}

export const useJackpotStore = defineStore<'jackpot', State>('jackpot', () => {
    const token = ref<string>('');
    const userInfo = ref<any>(null);
    const currentRouter = ref<string>('/userInfo');
    const permissions = ref<string[]>([]);
    const setToken = (value: string) => {
        token.value = value
    }
    const setUserInfo = (value: any) => {
        userInfo.value = value
    }
    const removeToken = () => {
        token.value = ''
        userInfo.value = null
        currentRouter.value = ''
    }

    return { token, userInfo, permissions, setToken, currentRouter, setUserInfo, removeToken }
}, {
    // @ts-ignore
    persist: {
        storage: localStorage,
        key: 'jackpot-store',
        // @ts-ignore
        paths: ['token'],
        serializer: {
            deserialize: parse,
            serialize: stringify,
        },
    }
})
